<?php
		require_once("Stomp.php");
		$con = new Stomp("tcp://10.51.235.167:61613?jms.optimizeAcknowledge=false");
		$con->connect("topicsender", "QniZ2S9NE6qN7KqJ");
		$con->subscribe("/queue/transactions", array('ack' => 'client','activemq.prefetchSize' => 1 ));
		$con->begin("jms-map-json");

		try {
			require_once("Stomp/Message/Map.php");
			$tag_id = trim($_GET["tag_id"]);
			if (!preg_match("/^\d*$/", $tag_id)) { 
				$return["code"]=15;
				$return["msg"]="tag_id只能是数字！";
			}
			$sid = trim($_GET["sid"]);
			if (!preg_match("/^\d*$/", $sid)) { 
				$return["code"]=15;
				$return["msg"]="sid只能是数字！";
			}
			$add_log_id = trim($_GET["add_log_id"]);
			if (!preg_match("/^\d*$/", $add_log_id)) { 
				$return["code"]=15;
				$return["msg"]="add_log_id只能是数字！";
			}
			$buyer_nick = htmlspecialchars(trim(urldecode($_GET["buyer_nick"])));
			if (!$buyer_nick) { 
				$return["code"]=15;
				$return["msg"]="必须有buyer_nick！";
			}
			if(isset($return)){
				throw new Exception($return["msg"]);
			}
			$timeout = (int)($_GET["timeout"]);
			$quere = trim($_GET["quere"]);
			$msgBody = array(
						"tag_id"=>$tag_id,
						"buyer_nick"=>$buyer_nick,
						"sid"=>$sid,
						"add_log_id"=>$add_log_id,
						"ts"=>time(),
						);
			$msgHeader["persistent"] = "true"; // VERY IMPORTANT : Because By default, Stomp produced messages are set to non-persistent.
			$msgHeader["transformation"] = "jms-map-json";
			$msgHeader["AMQ_SCHEDULED_DELAY"] = $timeout;//延时
			$mapMessage = new StompMessageMap($msgBody, $msgHeader);
			$con->send("/queue/".$quere, $mapMessage, array("transaction" => "jms-map-json"));
			$con->commit("jms-map-json");
			
			$return["code"]=200;
			$return["msg"]="成功发送消息";
			echo json_encode($return);
			
		} catch (Exception $e) {
			$con->abort("jms-map-json");
			$return["code"]=15;
			$return["msg"]=$e->getMessage();
			echo json_encode($return);
		}

		// disconnect
		$con->disconnect();
		
		/*
		if(msg instanceof BytesMessage){  
			BytesMessage bm = (BytesMessage) msg;  
			byte[] bys = null;  
			try {  
				bys = new byte[(int) bm.getBodyLength()];  
				bm.readBytes(bys);  
				responseXml = new String(bys);  
			} catch (Exception e) {  
				System.out.println(e.getMessage());
				e.printStackTrace();  
			}  
		}else{  
			TextMessage bm = (TextMessage) msg;  
  
			try {  
				responseXml = bm.getText();  
			} catch (Exception e) {  
				System.out.println(e.getMessage()); 
			}  
		}  
		
		org.apache.activemq.command.ActiveMQMapMessage cannot be cast to javax.jms.TextMessage
		
		*/